package com.forum.article.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.forum.article.entity.ForumArticle;
import com.forum.article.service.ForumArticleService;
import com.forum.article.vo.Result;
import com.forum.article.vo.param.ArticleParam;
import com.forum.article.vo.param.PageParams;
import com.forum.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/article/forumArticle")
public class ForumArticleController {
    @Autowired
    ForumArticleService forumArticleService;

    /**
     * 通过文章类型和文章标签分页查询文章
     * 如果都没有就返回所有文章
     * @param pageParams
     * @return
     */
    @PostMapping
    public R articleByTypeAndTag(@RequestBody PageParams pageParams) {
        return forumArticleService.articleByTypeAndTag(pageParams);
    }

//  最热文章
    @GetMapping("hot")
    public R hotForumArticle(){
        int limit = 5;
        return forumArticleService.hotForumArticle(limit);
    }

//    最新文章
    @GetMapping("new")
    public R newForumArticle() {
        int limit = 5;
        return forumArticleService.newForumArticle(limit);
    }


    @PostMapping("view/{id}")
    public Result findForumArticleById(@PathVariable("id") Long articleId){
        return forumArticleService.findForumArticleById(articleId);
    }

    //文章发布
    @PostMapping("publish")
    public R publish(@RequestBody ArticleParam articleParam){
        System.out.println("articleParam : " + articleParam);
//        return forumArticleService.publish(articleParam);
        return forumArticleService.publish(articleParam);
    }

    //文章的删除
    @GetMapping("deleteArticle/{id}")
    public R deleteArticle(@PathVariable Long id){
        forumArticleService.removeById(id);
        return R.ok();
    }

    //文章分页查询
    @GetMapping("pageArticle/{current}/{limit}")
    public R pageArticle(@PathVariable Long current,@PathVariable Long limit){
        Page<ForumArticle> page = new Page<>(current,limit);

            forumArticleService.page(page,null);


        Long total = page.getTotal();
        List<ForumArticle> records = page.getRecords();
        return R.ok().put("rows",records).put("total", total);
    }
}

